package com.cljz.makepassenger.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.present.activity.FeedbackP;
import com.cljz.makepassenger.utils.CommonUtils;
import com.cljz.makepassenger.utils.T;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;


/**
 * Created by Administrator on 2017/4/5 0005.
 */

public class FeedbackActivity extends BaseActivity<FeedbackP> {
    @BindView(R.id.edt_feedback)
    EditText edtFeedback;
    @BindView(R.id.txt_submit)
    TextView txtSubmit;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.activity_feedback)
    LinearLayout activityFeedback;


    @Override
    protected void initListener() {
        super.initListener();
        //点击返回箭头
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(1, TimeUnit.SECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                exit();
            }
        });
        //文本框发生改变，按钮变色是否可以点击
        RxTextView.textChanges(edtFeedback)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
            //CharSequence可读可写序列
            @Override
            public void call(CharSequence charSequence) {
                if (!TextUtils.isEmpty(charSequence.toString()) && charSequence.toString().length() != 0) {
                    txtSubmit.setBackgroundResource(R.drawable.btn_backgroud_black);
                    txtSubmit.setClickable(true);
                } else {
                    txtSubmit.setBackgroundResource(R.drawable.btn_backgroud_gray);
                    txtSubmit.setClickable(false);
                }
            }
        });
        //点击提交后数据要进行传输
        RxView.clicks(txtSubmit)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(3, TimeUnit.SECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                if (CommonUtils.isNetworkAvailable()) {
                    //提交判断
                    //.trim是清除空格
                    getP().Feedback(edtFeedback.getText().toString().trim());

                } else {
                    T.getInstance().showShort("请开启网络");
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        exit();
    }

    private void exit() {
        Router.newIntent(FeedbackActivity.this).to(MainActivity.class).putBoolean("showDrawer",true).launch();
        Router.pop(FeedbackActivity.this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initPermission();
        tvTitleName.setText("反馈交流");
    }

    //进行布局加载
    @Override
    public int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    public FeedbackP newP() {
        return new FeedbackP();
    }

    public void FeedbackFail(String err) {
        dismissLoadingDialog();
        T.getInstance().showShort(err);
        edtFeedback.getText().clear();
    }

    public void FeedbackSucceed() {
        dismissLoadingDialog();
        T.getInstance().showShort("反馈意见提交成功");
        edtFeedback.getText().clear();
        finish();
    }

}
